//
//  FKCouponListVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/2.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKCouponListVC.h"
#import "FKCouponListCell.h"
#import "FKOrder.h"
#import "FKPayMethodView.h"
#import <AlipaySDK/AlipaySDK.h>
#import <WXApi.h> // 微信支付
#import "FKPayResultVC.h"
#import "FKCoupon.h"

@interface FKCouponListVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableW;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *param;
@property(nonatomic, assign) NSInteger page;

@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKCouponListVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableW.constant = fkScreenW;
    self.param[@"status"] = self.status;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.autoresizingMask = UIViewAutoresizingNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Coupon"]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [_emptyView addSubview:label];
        label.font = fk_adjustFont(16);
        label.text = @"还没有优惠券哦~";
        label.textColor = fkColor999999;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
        }];
    }
    return _emptyView;
}

- (void)refreshData
{
    if (self.datas.count == 0) [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCouponListCell *cell = [FKCouponListCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    cell.high = [self.status isEqualToString:@"1"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(85);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

#pragma mark - 网络请求
- (void)loadData
{
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=coupon_list" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            if (self.refreshTopTitles) {
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                dict[@"notUsed"] = result.data[@"counta"];
                dict[@"AlreadyUsed"] = result.data[@"countb"];
                dict[@"Expired"] = result.data[@"countc"];
                self.refreshTopTitles(dict);
            }
            
            self.datas = [FKCoupon mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            
//            // 测试数据
//            FKCoupon *coupon = [FKCoupon new];
//            coupon.high = true;
//            NSString *price = @"$100.00";
//            coupon.priceStr = [price fk_addAttributeStyle:@"$" color:fkMainColor font:fk_adjustFont(12)];
//            coupon.title = @"xxxxxxxx";
//            coupon.startTime = @"2019-03-30";
//            coupon.endTime = @"2019-03-30";
//            coupon.status = @"1";
//            coupon.statusName = @"未使用";
//            self.datas = @[coupon];
            
            [self.tableView reloadData];
            if (self.datas.count > 0) self.page++;
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}

- (void)loadMoreData
{
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=coupon_list" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_footer endRefreshing];
        if (result.isSuccess) {
            NSArray *newArray = [FKCoupon mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            if (newArray.count > 0) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
                [array addObjectsFromArray:newArray];
                self.datas = array;
                [self.tableView reloadData];
                self.page++;
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}



@end
